<?php 
/**
* 数据库操作类 pdo扩展
*/
class Model
{
	
	public $db;
	function __construct()
	{
		try { 
		//链接数据库
		$this->db = new PDO(C('DB_TYPE').":host=".C('DB_HOST').";dbname=".C('DB_NAME').";charset=utf8",C('DB_USERNAME'),C('DB_PASSWORD'));

		}catch(PDOException $e){ 
			// // var_dump($db->errorCode());
			// var_dump($e->getmessage());exit;
			exit("错误代码：".$e->getCode().",错误信息".$e->getmessage());
		}
	
	}


	//插入行的方法做不出来
	public function add($data,$tablename)
	{
		// $insert="insert into ".$tablename ." (name,age) values(".$data['name'].",".$data['age'].")";

		// $handle=$this->db->exec($insert);
		// $errorCode=intval($this->db->errorCode());
		// if ($errorCode) {
		// 	var_dump($this->db->errorInfo());
		// 	exit('错误代码：'.$this->db->errorCode());

		// }
	}

	//查询函数
	public function query($tablename3,$list)
	{
		$sql = "select ".$list. " from " .$tablename3;
		$res=$this->db->query($sql);			//通过查询，返回一个结果集对象
		$arr=$res->fetchAll(PDO::FETCH_ASSOC);	//取结果集中的数据
		return $arr;							//将得到的数组返回给函数
	}

	//删除行的函数
	public function delete($tablename2,$where2)
	{
		$delete = "delete from ".$tablename2. " where " .$where2 ;
		$res=$this->db->exec($delete);
		
	}


	//修改行的函数
	public function update($tablename1,$renew,$where)
	{
		$a = " update ". $tablename1. " set " .$renew ." where ". $where;
		$res=$this->db->exec($a);
		//先将错误代码转换成整形，其原来是字符串类型，字符串永远都是true。
		$errorCode=intval($this->db->errorCode());
		if ($errorCode) {
			var_dump($this->db->errorInfo());
			exit('错误代码：'.$this->db->errorCode());

		}
		
	}


	//修改表名的函数
	public function rename($tablename_old,$tablename_new)
	{
		$table="alter table ".$tablename_old." rename ".$tablename_new;
		$res=$this->db->exec($table);
		$errorCode=intval($this->db->errorCode());
		if ($errorCode) {
			var_dump($this->db->errorInfo());
			exit('错误代码：'.$this->db->errorCode());
		}
	}


	//修改字段同时更名
	public function relistname($listname)
	{
		if (!array_key_exists('decorate', $listname)) {
			$listname['decorate']='';
		}
		$list="alter table ".$listname['tablename']." change ".$listname['listname_old'].' '. $listname['listname_new'].' '. $listname['type'].' '.$listname['decorate'];
		$res=$this->db->exec($list);
		$errorCode=intval($this->db->errorCode());
		if ($errorCode) {
			var_dump($this->db->errorInfo());
			exit('错误代码：'.$this->db->errorCode());
		}
	}































}
 ?>